package org.springblade.task.job.report.user;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.service.IBaseAudioChapterOrderService;
import org.springblade.task.base.service.IBaseEbookOrderService;
import org.springblade.task.base.service.IBaseNetChapterOrderService;
import org.springblade.task.base.service.IBaseUserService;
import org.springblade.task.base.vo.BaseUserVO;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Title: 潜力用户
 * @version V1.0
 */
@Component
public class DailyPotentialUserJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseUserService baseUserService;
	@Autowired
	private IBaseEbookOrderService baseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService baseAudioChapterOrderService;
	@Autowired
	private IBaseNetChapterOrderService baseNetChapterOrderService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("DailyPotentialUserJob")
	public ReturnT<String> DailyPotentialUserJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyPotentialUserJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyPotentialUserJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		//
		List<BaseUserVO> baseUserVOList = baseUserService.queryPotentialUserList();

		BaseOrderBookParam param = new BaseOrderBookParam();
		Date startDate = DateUtils.parseStrToDate(taskDate, DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		String startTime = DateUtils.parseDateToStr(DateUtils.getDateBefore(startDate, 365), DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
		param.setStartDate(startTime);
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		List<String> ebookOrderPinList = baseEbookOrderService.queryDateRangePayUserList(param);
		//
		List<String> audioChapterOrderPinList = baseAudioChapterOrderService.queryDateRangePayUserList(param);
		//
		List<String> netChapterOrderPinList = baseNetChapterOrderService.queryDateRangePayUserList(param);

		ebookOrderPinList.addAll(audioChapterOrderPinList);
		ebookOrderPinList.addAll(netChapterOrderPinList);
		Set<String> pinSet = new HashSet(ebookOrderPinList);
		List<String> updatePotentialUserStateOneList = new ArrayList<>();
		List<String> updatePotentialUserStateTwoList = new ArrayList<>();
		for(BaseUserVO userVO: baseUserVOList) {
			if(!pinSet.contains(userVO.getPin())){
				updatePotentialUserStateOneList.add(userVO.getPin());
			} else {
				updatePotentialUserStateTwoList.add(userVO.getPin());
			}
		}
		baseUserService.updatePotentialUserStateOne(updatePotentialUserStateOneList);
		baseUserService.updatePotentialUserStateTwo(updatePotentialUserStateTwoList);

	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
